package com.baichang.taoshunda.controller.mobile;

import com.baichang.framework.controller.BaseMobileController;
import com.baichang.framework.controller.IService;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.controller.ControllerConstants;
import com.baichang.taoshunda.service.MsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 短信控制器
 * Created by Dylan on 2017/5/25.
 */
@Controller
@RequestMapping(ControllerConstants.MMSG)
public class MsgMController extends BaseMobileController {

    @Autowired
    private MsgService msgService;

    /**
     * 根据手机号获取验证码
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getMobileCode")
    public void getMobileCode(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String phone = getString(json, "phone", "");
                return msgService.getCodeByPhone(phone);
            }
        });
    }

    /**
     * 手机号登陆获取验证码
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getMobileLoginCode")
    public void getMobileLoginCode(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String phone = getString(json, "phone", "");
                return msgService.getMobileLoginCode(phone);
            }
        });
    }

    /**
     * 获取忘记密码验证码
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getForeignPwdCode")
    public void getForeignPwdCode(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String phone = getString(json, "phone", "");
                return msgService.getForeginPwdCode(phone);
            }
        });
    }

    /**
     * 验证注册验证码
     *
     * @param request
     * @param response
     */
    @RequestMapping("/checkRegisterCode")
    public void checkRegisterCode(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String phone = getString(json, "phone", "");
                String code = getString(json, "code", "");
                return msgService.checkRegisterCode(phone, code);
            }
        });
    }

    /**
     * 验证忘记密码验证码(验证码验证通用)
     *
     * @param request
     * @param response
     */
    @RequestMapping("/checkForeignPwdCode")
    public void checkForeignPwdCode(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String phone = getString(json, "phone", "");
                String code = getString(json, "code", "");
                return msgService.checkForeignPwdCode(phone, code);
            }
        });
    }

    /**
     * 手机号获取验证码
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getCodeByPhone")
    public void getCodeByPhone(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String phone = getString(json, "phone", "");
                return msgService.getCodeByUserPhone(phone);
            }
        });
    }

}
